//
//  TotalRecordViewController.m
//  EasyToTesco2.0
//
//  Created by baozi on 16/8/16.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "TotalRecordViewController.h"
#import "TotalRecordSelectedView.h"
#import "PointTotalRecord.h"
#import "ActivityTotalRecord.h"
#import "TotalRecordWholeAndPointTableViewCell.h"
#import "ActivityTotalRecordTableViewCell.h"
#import "PointTotalRecord.h"
#import "ActivityTotalRecord.h"

@interface TotalRecordViewController ()<UITableViewDataSource,didClickTotalRecordSelectedView,UITableViewDelegate>

{
    NSInteger _totalRecordType;
    
    TotalRecordSelectedView *totalSelectedView;
    
    UITableView *totalTableView;
    
    NSInteger _fkfs;
    
    NSMutableArray *tempArray;
    
    MBProgressHUD *progressHUD;
    
    NSInteger judgeSelected;
    
    NSInteger _refreshTotalRecord;
}

@property (nonatomic,strong)NSMutableArray *totalRecordArray;

@end

@implementation TotalRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消费记录";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    tempArray = [NSMutableArray array];
    
    [self loadTotalRecordSelectedView];
    
    [self loadTableView];
    
    [self addPullUpdateData];
    
    judgeSelected = 1000;
    
    _refreshTotalRecord = 1;
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"usertype":@1,
                             @"fkfs":@"2",
                             @"p":@1,
                             @"type":@"2"
                             };
    
    
    progressHUD = [[MBProgressHUD alloc] init];
    progressHUD.mode =MBProgressHUDModeIndeterminate;
    progressHUD.color = [UIColor blackColor];
    progressHUD.activityIndicatorColor = [UIColor orangeColor];
    progressHUD.dimBackground = false;
    [progressHUD show:YES];
    [self.view addSubview:progressHUD];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loadTotalRecordData:params andType:2];

    });
    
    
    
}

#pragma mark 数据请求
- (void)loadTotalRecordData:(NSDictionary *)params andType:(NSInteger)fkfs {
    
    [tempArray removeAllObjects];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"%@",params);
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Hz/Gethz" completeBlock:^(id object) {
            NSLog(@"%@",object);
            if(JudgeBackDataCode) {
                NSArray *arr = object[@"data"];
                [totalTableView.mj_footer endRefreshing];
                if (fkfs == 2) {
                    for (NSDictionary *dict in arr) {
                        PointTotalRecord *point = [[PointTotalRecord alloc] initWithDictionary:dict error:nil];
                        if (point) {
                            [tempArray addObject:point];
                        }
                    }
                } else if (fkfs == 3) {
                    for (NSDictionary *dict in arr) {
                        PointTotalRecord *point = [[PointTotalRecord alloc] initWithDictionary:dict error:nil];
                        if (point) {
                            [tempArray addObject:point];
                        }
                    }
                } else {
                    for (NSDictionary *dict in arr) {
                        ActivityTotalRecord *activity = [[ActivityTotalRecord alloc] initWithDictionary:dict error:nil];
                        if (activity) {
                            [tempArray addObject:activity];
                        }
                    }
                }
                
                if(_fkfs == 4) {
                    for (ActivityTotalRecord *activity in tempArray) {
                        [self.totalRecordArray addObject:activity];
                    }
                } else {
                    for (PointTotalRecord *point in tempArray) {
                        [self.totalRecordArray addObject:point];
                    }
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    totalSelectedView.userInteractionEnabled = YES;
                    [totalTableView reloadData];
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [progressHUD hide:YES];
                    NSDictionary *dict = [object objectForKey:@"message"];
                    MBProgressHUD *hud = [MBProgressHUD showMessage:dict[@"view"] toView:self.view dimBackground:NO AndCompleteBlock:nil];
                    [self.view addSubview:hud];
                    totalSelectedView.userInteractionEnabled = YES;
                    [totalTableView reloadData];
                    [totalTableView.mj_footer endRefreshingWithNoMoreData];
                });
            }
        }];
    });
}

#pragma mark 添加控件
- (void)loadTotalRecordSelectedView {
    
    totalSelectedView = [[TotalRecordSelectedView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.07)];
    
    totalSelectedView.delegate = self;
    
    [self.view addSubview:totalSelectedView];

}

- (void)loadTableView {
    CGFloat y = CGRectGetMaxY(totalSelectedView.frame);
    totalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, WIDTH, HEIGHT - y - 64)style:UITableViewStylePlain];
    totalTableView.delegate = self;
    totalTableView.dataSource = self;
    totalTableView.backgroundColor = BACKGROUND_COLOR;
    totalTableView.tableFooterView = [[UIView alloc] init];
    totalTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:totalTableView];
    
    _totalRecordType = 1000;
    _fkfs = 2;
    
    
}

#pragma mark 上拉刷新
- (void)addPullUpdateData
{
    totalTableView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTotalRecordData)];
}

- (void)loadMoreTotalRecordData {
    _refreshTotalRecord++;
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"usertype":@1,
                             @"fkfs":[NSString stringWithFormat:@"%ld",_fkfs],
                             @"p":[NSString stringWithFormat:@"%ld",_refreshTotalRecord],
                             @"type":@"2"
                             };
    [self loadTotalRecordData:params andType:_fkfs];
    
}


#pragma mark Delegate
- (void)selectBtnClick:(UIButton *)button {
    if (judgeSelected == button.tag) {
        NSLog(@"%s",__FUNCTION__);
        return;
    }
    
    [totalTableView.mj_footer resetNoMoreData];
    
    _refreshTotalRecord = 1;
    _totalRecordType = button.tag;
    judgeSelected = button.tag;
    [self.totalRecordArray removeAllObjects];
    [totalTableView reloadData];
    
    progressHUD = [[MBProgressHUD alloc] init];
    progressHUD.mode =MBProgressHUDModeIndeterminate;
    progressHUD.color = [UIColor blackColor];
    progressHUD.activityIndicatorColor = [UIColor whiteColor];
    progressHUD.dimBackground = false;
    [progressHUD show:YES];
    [self.view addSubview:progressHUD];
    
    NSString *fkfs;
    
    switch (_totalRecordType) {
        case 1000:
//            NSLog(@"现金");
            fkfs = @"2";
            break;
            
        case 1001:
//            NSLog(@"积分");
            fkfs = @"3";
            break;
            
        case 1002:
//            NSLog(@"活动");
            fkfs = @"4";
            break;
            
        default:
            break;
    }
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"usertype":@1,
                             @"fkfs":fkfs,
                             @"p":@1,
                             @"type":@"2"
                             };
    
    _fkfs = fkfs.integerValue;
    totalSelectedView.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loadTotalRecordData:params andType:fkfs.integerValue];
    });
}

#pragma mark TableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.totalRecordArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [progressHUD hide:YES];
    
    if (_totalRecordType == 1000) {
        
        TotalRecordWholeAndPointTableViewCell *whole = [tableView dequeueReusableCellWithIdentifier:@"whole"];
        if (!whole) {
            whole = [[TotalRecordWholeAndPointTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"whole"];
            whole.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        PointTotalRecord *pointModel = self.totalRecordArray[indexPath.row];
        whole.pointModel = pointModel;
        
        return whole;
    } else if (_totalRecordType == 1001) {
        
        TotalRecordWholeAndPointTableViewCell *point = [tableView dequeueReusableCellWithIdentifier:@"point"];
        if (!point) {
            point = [[TotalRecordWholeAndPointTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"point"];
            point.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        PointTotalRecord *pointModel = self.totalRecordArray[indexPath.row];
        point.pointModel = pointModel;
        
        return point;
    } else {
        ActivityTotalRecordTableViewCell *activity = [tableView dequeueReusableCellWithIdentifier:@"activity"];
        
        if (!activity) {
            activity = [[ActivityTotalRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"activity"];
            activity.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        ActivityTotalRecord *activityModel = self.totalRecordArray[indexPath.row];
        activity.activity = activityModel;
        
        return activity;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}


#pragma mark 懒加载
- (NSMutableArray *)totalRecordArray {
    if (!_totalRecordArray) {
        _totalRecordArray = [NSMutableArray array];
    }
    return _totalRecordArray;
}

- (void)dealloc {
    [progressHUD hide:YES];
    [progressHUD removeFromSuperViewOnHide];
}

@end
